let { HttpRequest, globalData, UserAccess, addUserStat, mfutil } = getApp();
const app = getApp();
Page({
  data: {
    id: null,
    type: null,
    userActive: null,
    radioActive: false,
    router: '/pages/conversion-page/conversion-page',
    pagetitle: '实物兑换详情页',
    scrolly: true,
    scrollTop: 0,
    loadingActive: false
  },
  onLoad(options) {
    my.hideShareMenu({
      success() {
      },
      fail() {
      },
    });
    let { id, listbuttontext } = options;
    this.setData({
      id, listbuttontext
    })
    this.initPage()
    /**
* 用户行为记录埋点
*/
    let routerArr = getCurrentPages();
    let params = app.globalData.params;
    let channel = app.globalData.channel;
    let UserAccessoptions = {
      grade: routerArr.length > 1 ? routerArr[routerArr.length - 2].data.router : '',
      name: routerArr[routerArr.length - 1].data.pagetitle,
      address: options.id,
    }
    let track = UserAccess(UserAccessoptions)
    // 自定义埋点
    let customAccessoptions = {
      customType: 'TileCover_' + options.id,
      // tileId: id
    }
    console.log(id, '进入Tile封面');
    let tracks = addUserStat(customAccessoptions)
  },
  // onShow() {
  //   this.initPage()
  // },
  async initPage() {
    let { id } = this.data;
    let path = `/vans-family/api/v2.1/project/vansFamily/cms/forward/get?url=/v2/cms/api/tile/${id}`;
    let parmasObj = {
      member_id_type: 1,
      member_id: globalData.userInfo.memberId
    }
    this.setData({
      loadingActive: true
    })
    let res = await HttpRequest(path, 'POST', parmasObj, {});
    console.log(res,"-----------------------------");
    let bodyObj2 = {
      // "param": "13800138000",
      // "type": 0,
      "param": globalData.ouid,
      "type": 3
    }
    let path2 = `/vans-family/api/v2.1/project/vansFamily/clm/forward/sendjson/get?url=/v1/points/api/tmall/account`;
    let res2 = await HttpRequest(path2, 'POST', '', bodyObj2);
    console.log(res2, '积分查询积分查询积分查询积分查询积分查询')
    if (res2.data.resultCode == 0) {
      globalData.userInfo.availablePoint = res2.data.data.availablePoint;
      globalData.userInfo.willExpirePoint = res2.data.data.willExpirePoint;
    }
    if (res.data.code == 0) {
      let tilesDetail = res.data.data;
      console.log("2222222222222223",tilesDetail);
      let { availablePoint } = globalData.userInfo;
      let { points, type } = tilesDetail.tile_info;
      let userActive = null;
      userActive = (availablePoint >= points) || points == 0 ? true : false;    //用户可用积分/活动所需积分
      console.log(userActive, 'userActiveuserActiveuserActive')
      console.log(availablePoint, '用户可以积分========活动所需积分', points);
      let json = res.data.data;
      this.setData({
        type: res.data.data.tile_info.type,
        json,
        userActive,
        skuid: res.data.data.tile_info.skuid,
      })
      if (json.tile_info.deliver_way == 'express' || json.tile_info.deliver_way == 'all') {
        let kpath = `/vans-family/api/v2.1/project/vansFamily/item/getBySkuid?skuid=${res.data.data.tile_info.skuid}`
        let kucun = await HttpRequest(kpath, 'GET', {}, {});
        this.setData({
          quantity: kucun.data ? kucun.data.quantity : 0
        })
        console.log(this.data.quantity,"cccccccccccccccccccccccccccccccc")
      }
      if (json.tile_info.deliver_way == 'store' || json.tile_info.deliver_way == 'all') {
        let storespath = `/vans-family/api/v2.1/project/vansFamily/cms/forward/get?url=/v1/cms/api/get_available_stores`;
        let storesbodyObj = {
        }
        let storesparmasObj = {
          member_id_type: 1,
          member_id: globalData.userInfo.memberId,
          tile_id: id
        }
        console.log(storesparmasObj,"-----------------------");
        let storesres = await HttpRequest(storespath, 'POST', storesparmasObj, storesbodyObj);
        console.log(storesres,"0000000000000000000000000000000000000");
        if (storesres.data.code == 0) {
          this.setData({
            storeList: storesres.data.data
          })
        }
      }

    }
    this.setData({
      loadingActive: false
    })
  },
  /**
   * 门店自提
   * @param {*} event 
   */
  pick() {
    let { id, type, skuid, storeList } = this.data;
    if (storeList.length == 0) {
      my.alert({
        content: '库存不足，无法参与兑礼!'
      })
      return
    }
    my.navigateTo({
      url: `/pages/conversion-form/conversion-form?method=pick&id=${id}&type=${type}&skuid=${skuid}&active=0`
    });
    /**
   * 用户行为记录埋点
   */
    let routerArr = getCurrentPages();
    let params = app.globalData.params;
    let channel = app.globalData.channel;
    let options = {
      grade: routerArr.length > 1 ? routerArr[routerArr.length - 2].data.router : '',
      name: routerArr[routerArr.length - 1].data.pagetitle,
      address: routerArr[routerArr.length - 1].data.router,
      String4: params ? `params=${params.join(',')}&` : '' + `tileid=${this.data.id}${channel ? '&channel=' + channel : ''}`,
      String3: '门店自提'
    }
    let track = UserAccess(options)
  },
  /**    
   * 快递到家
   */
  expressage() {
    let { id, type, skuid, quantity } = this.data;
    if (quantity <= 0) {
      my.alert({
        content: '库存不足，无法参与兑礼!'
      })
      return
    }
    my.navigateTo({
      url: `/pages/conversion-form/conversion-form?method=expressage&id=${id}&type=${type}&skuid=${skuid}`
    });
    /**
   * 用户行为记录埋点
   */
    let routerArr = getCurrentPages();
    let params = app.globalData.params;
    let channel = app.globalData.channel;
    let options = {
      grade: routerArr.length > 1 ? routerArr[routerArr.length - 2].data.router : '',
      name: routerArr[routerArr.length - 1].data.pagetitle,
      address: routerArr[routerArr.length - 1].data.router,
      String4: params ? `params=${params.join(',')}&` : '' + `tileid=${this.data.id}${channel ? '&channel=' + channel : ''}`,
      String3: '快递到家'
    }
    let track = UserAccess(options)
  },
  /**
   * 赚积分跳转
   */
  getIntegralTo() {
    my.alert({
      content: '您的积分不足，无法参与兑礼！'
    })
  },
  //单选协议同意/默认
  radioChange() {
    let { radioActive } = this.data;
    this.setData({
      radioActive: !radioActive
    })
  },
  /**
 * 链接文字点击
 */
  hyperlinkChange() {
    let { hyperlinkAcitve } = this.data;
    this.setData({
      hyperlinkAcitve: !hyperlinkAcitve
    })
  },
  onScroll(event) {
    this.setData({
      scrollTop: event.detail.scrollTop,
      ScrollToUpper: scrollTop == 0 ? true : false
    })
    if (scrollTop == 0) {
      this.setData({
        ScrollToUpper: true
      })
    }
  },
  onTouchStart(event) {
    console.log(event, 'onTouchStartonTouchStart')
    this.setData({
      starty: event.changedTouches[0].pageY,
    })
  },
  onTouchMove(event) {
    //当前pagey
    this.setData({
      movey: event.changedTouches[0].pageY
    })
    let { scrollTop } = this.data;
    let { ScrollToUpper, ScrollToLower, movey, starty } = this.data;
    if (ScrollToUpper && (starty > movey)) {
      this.setData({
        scrolly: true,
        ScrollToUpper: false,
        ScrollToLower: false
      })
    } else
      if (ScrollToUpper && (starty < movey)) {
        this.setData({
          scrolly: false,
        })
      }
    if (ScrollToLower && (starty < movey)) {
      this.setData({
        scrolly: true,
        ScrollToUpper: false,
        ScrollToLower: false
      })
    } else
      if (ScrollToLower && (starty > movey)) {
        this.setData({
          scrolly: false,
        })
      }
  },
  onScrollToUpper(event) {
    this.setData({
      scrolly: false,
      ScrollToUpper: true,
      ScrollToLower: false
    })
  },
  onScrollToLower() {
    this.setData({
      scrolly: false,
      ScrollToLower: true,
      ScrollToUpper: false
    })
  }
});
